#!/bin/bash


stf_secret="admin@20201230+="
admin_name="admin@20201230+="
admin_email="admin@qq.com"
root_group="admin"
local_ip=172.29.226.144
public_ip=8.135.28.12

db_port=28015
container_port=3000
storage_port=3500
apk_port=3300
image_port=3400
websocket_port=3600
api_port=3700
app_port=3100
auth_port=3200

app_pub_port=7150
app_dealer_port=7160
app_pull_port=7170

dev_pub_port=7250
dev_dealer_port=7260
dev_pull_port=7270

image=mystf/stf
offical_image=openstf/stf


a_start() {
    # rethinkdb
    docker run -d --name rethinkdb -v /srv/rethinkdb:/data          \
        -e "AUTHKEY=$stf_secret" --net host                         \
        rethinkdb rethinkdb --bind all                              \
        --canonical-address $local_ip                               \
        --cache-size 8192 --no-update-check

    # stf-migrate
    docker run -d --name stf-migrate                                \
        -e RETHINKDB_PORT_28015_TCP=tcp://$local_ip:$db_port        \
        -e "STF_ROOT_GROUP_NAME=$root_group"                        \
        -e "STF_ADMIN_NAME=$admin_name"                             \
        -e "STF_ADMIN_EMAIL=$admin_email"                           \
        $offical_image stf migrate

    # stf-storage-temp
    docker run -d --name stf-storage-temp                           \
        -v /mnt/storage:/data -p $storage_port:$container_port      \
        $image stf storage-temp                                     \
        --port $container_port --save-dir /data

    # stf-storage-plugin-apk
    docker run -d --name stf-storage-plugin-apk                     \
        -p $apk_port:$container_port                                \
        $image stf storage-plugin-apk --port $container_port        \
        --storage-url http://$local_ip:$storage_port

    # stf-storage-plugin-image
    docker run -d --name stf-storage-plugin-image                   \
        -p $image_port:$container_port                              \
        $image stf storage-plugin-image --port $container_port      \
        --storage-url http://$local_ip:$storage_port

    # stf-triproxy-app
    docker run -d --name stf-triproxy-app                           \
        --net host $image stf triproxy app                          \
        --bind-pub "tcp://*:$app_pub_port"                          \
        --bind-dealer "tcp://*:$app_dealer_port"                    \
        --bind-pull "tcp://*:$app_pull_port"

    # stf-triproxy-dev
    docker run -d --name stf-triproxy-dev                           \
        --net host $image stf triproxy dev                          \
        --bind-pub "tcp://*:$dev_pub_port"                          \
        --bind-dealer "tcp://*:$dev_dealer_port"                    \
        --bind-pull "tcp://*:$dev_pull_port"

    # stf-processor
    docker run -d --name stf-processor                              \
        -e RETHINKDB_PORT_28015_TCP=tcp://$local_ip:$db_port        \
        $image stf processor stf-processor                          \
        --connect-app-dealer tcp://$local_ip:$app_dealer_port       \
        --connect-dev-dealer tcp://$local_ip:$dev_dealer_port

    # stf-websocket
    docker run -d --name stf-websocket                              \
        -e RETHINKDB_PORT_28015_TCP=tcp://$local_ip:$db_port        \
        -e "SECRET=$stf_secret"                                     \
        -p $websocket_port:$container_port                          \
        $image stf websocket --port $container_port                 \
        --storage-url http://$local_ip:$storage_port                \
        --connect-sub tcp://$local_ip:$app_pub_port                 \
        --connect-push tcp://$local_ip:$app_pull_port

    # stf-api
    docker run -d --name stf-api                                    \
        -e RETHINKDB_PORT_28015_TCP=tcp://$local_ip:$db_port        \
        -e "SECRET=$stf_secret"                                     \
        -p $api_port:$container_port                                \
        $image stf api --port $container_port                       \
        --connect-sub tcp://$local_ip:$app_pub_port                 \
        --connect-push tcp://$local_ip:$app_pull_port               \
        --connect-sub-dev tcp://$local_ip:$dev_pub_port             \
        --connect-push-dev tcp://$local_ip:$dev_pull_port

    # stf-reaper
    docker run -d --name stf-reaper                                 \
        -e RETHINKDB_PORT_28015_TCP=tcp://$local_ip:$db_port        \
        $image stf reaper dev                                       \
        --connect-push tcp://$local_ip:$dev_pull_port               \
        --connect-sub tcp://$local_ip:$app_pub_port                 \
        --heartbeat-timeout 30000

    # stf-groups-engine
    docker run -d --name stf-groups-engine                          \
        -e RETHINKDB_PORT_28015_TCP=tcp://$local_ip:$db_port        \
        -e "SECRET=$stf_secret"                                     \
        $image stf groups-engine                                    \
        --connect-sub tcp://$local_ip:$app_pub_port                 \
        --connect-push tcp://$local_ip:$app_pull_port               \
        --connect-sub-dev tcp://$local_ip:$dev_pub_port             \
        --connect-push-dev tcp://$local_ip:$dev_pull_port

    # stf-app
    docker run -d --name stf-app                                    \
        -e RETHINKDB_PORT_28015_TCP=tcp://$local_ip:$db_port        \
        -e "SECRET=$stf_secret"                                     \
        -p $app_port:$container_port $image stf app                 \
        --port $container_port                                      \
        --auth-url http://$public_ip/auth/mock/                     \
        --websocket-url ws://$public_ip/

    # stf-auth-mock
    docker run -d --name stf-auth-mock                              \
        -e RETHINKDB_PORT_28015_TCP=tcp://$local_ip:$db_port        \
        -e "SECRET=$stf_secret"                                     \
        -p $auth_port:$container_port $image stf auth-mock          \
        --port $container_port                                      \
        --app-url http://$public_ip/
}

remove() {
    # stop
    docker stop                            \
        stf-auth-mock                      \
        stf-app                            \
        stf-groups-engine                  \
        stf-reaper                         \
        stf-api                            \
        stf-websocket                      \
        stf-processor                      \
        stf-triproxy-dev                   \
        stf-triproxy-app                   \
        stf-storage-plugin-image           \
        stf-storage-plugin-apk             \
        stf-storage-temp                   \
        rethinkdb

    # rm
    docker rm                              \
        stf-auth-mock                      \
        stf-app                            \
        stf-groups-engine                  \
        stf-reaper                         \
        stf-api                            \
        stf-websocket                      \
        stf-processor                      \
        stf-triproxy-dev                   \
        stf-triproxy-app                   \
        stf-storage-plugin-image           \
        stf-storage-plugin-apk             \
        stf-storage-temp                   \
        stf-migrate                        \
        rethinkdb

    rm /srv/rethinkdb -rf &> /dev/null
    rm /mnt/storage -rf &> /dev/null
}

${1-a_start}
